package com.mlnx.web.data.controller;


import com.mlnx.common.entity.Response;
import com.mlnx.common.entity.ResponseData;
import com.mlnx.common.form.PageForm;
import com.mlnx.data.entity.SugarInfo;
import com.mlnx.data.pojo.form.SugarInfoForm;
import com.mlnx.data.service.SugarDataService;
import com.mlnx.stream.service.DeviceBroadcastService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.shan.spring.base.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


@Api(value = "血糖数据接口", tags = {"血糖数据接口"})
@RestController
@RequestMapping("mlnx_data/sugar")
public class SugarController extends BaseController {
    @Autowired
    private SugarDataService sugarDataService;

    @Autowired
    private DeviceBroadcastService deviceBroadcastService;


    @ApiOperation("获取血糖数据")
    @GetMapping(value = "/sugar")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startTime", value = "起始时间", required = true),
            @ApiImplicitParam(name = "endTime", value = "结束时间", required = true),
            @ApiImplicitParam(name = "patientId", value = "患者ID", required = true)
    })
    public Response getSugars(@RequestParam("startTime") Long startTime, @RequestParam("endTime") Long endTime,
                              @RequestParam("patientId") Integer patientId) {

        return result(sugarDataService.getSugars(patientId, startTime, endTime));
    }

    @ApiOperation("更新血糖数据")
    @PutMapping(value = "")
    public Response updateSugars(@Valid SugarInfo sugarInfo) {
        sugarDataService.updateSugar(sugarInfo);
        return result();
    }

    @ApiOperation("删除血糖数据")
    @DeleteMapping(value = "")
    public Response deleteSugar(@RequestBody List<SugarInfo> sugarInfos) {
        sugarDataService.deleteSugars(sugarInfos);

        return result();
    }

    @ApiOperation("分页获取所有血糖数据")
    @GetMapping(value = "")
    public ResponseData getAllSugars(@Valid PageForm pageForm, @Valid SugarInfoForm sugarInfoForm) {

        return result(sugarDataService.getAllSugars(pageForm, sugarInfoForm));
    }

    @ApiOperation("新增血糖数据")
    @PostMapping(value = "")
    public Response addSugar(@Valid SugarInfo sugarInfo) {
        deviceBroadcastService.castSugarInfo(sugarInfo);
        return result();
    }
}
